/*  Novelties: A visual novel programming language based on JavaFX
    Copyright (C) 2008  Hao Xu

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * /
/*
 * Branch.fx
 *
 * Created on Apr 14, 2008, 7:41:52 PM
 */

package avgengine.script;

/**
 * @author ertri
 */

class Branch {
    attribute script:Action*;
    
    attribute name:String;
    
    operation runScript(s:ScriptedScene,h:operation(scene:String));
    
    operation runScriptFrom(i:int, s:ScriptedScene,h:operation(scene:String));
}


operation Branch.runScriptFrom(off:int, s:ScriptedScene, h:operation(scene:String)) {
    var i= off;
    println("number of paragraphs to go {sizeof this.script - i}");
    while(i<sizeof this.script) {
        println("paragraph {i}");
        if(script[i].asynchronous) {
            println("doAsynch");
            script[i].doAsynch(s,
            operation(scene:String) {
                if(scene==null) {
                    runScriptFrom(i+1,s,h);
                } else {
                    var b = s.selectBranch(scene);
                    println("selected branch: {scene}");
                    if(b <> null) {
                        println("script from selected branch");
                        b.runScript(s,
                        operation(scene:String) {
                            if(scene==null) {
                                runScriptFrom(i+1,s,h);
                            } else {
                                h(scene);
                            }
                        });
                    } else {
                        h(scene);
                    }
                }
            });
            return;
        }else {
            script[i].doSynch(s);
        }
        i++;
    }
    h(null);
}
operation Branch.runScript(s,h) {
    
    runScriptFrom(0,s,h);
}